package org.remixlab.gnutopic.pojo;

import java.util.ArrayList;
import java.util.Collection;
import java.util.Date;
import java.util.Iterator;
import java.util.TreeMap;

import org.apache.commons.lang3.StringEscapeUtils;

import codeanticode.glgraphics.GLModel;

/**
 * Representa una categoría de un foro
 * @author mauro
 *
 */

public class ForumCategory {
	
	private String name;
	
	private ForumCategory parentCategory; 	//categoría a la que pertenece esta sub categoría
	
	private ArrayList <ForumCategory> subCategories;
	private TreeMap <String,ForumThread> threads;
	private TreeMap <Date, ForumThread> threadsByDate;
	
	private int id = -1;
	
	private int messagesNumber = 0;	//numero de mensajes publicados en esta categoría
	
	//inicializo los colores... se representa el color en hsb
	private float hue = 0;	//número que indica el color a representar...
	private float sat = 0;	//la saturación del color...
							//Nota: el brillo depende del del hilo.. por eso no esta en esta clase.
	
	private GLModel model;	//el modelo de esta categoría.
	private float[] coords;	//almacena las coordenadas de los mensajes de la categoría
	private float[] colors;	//almacena los colores para los mensajes de la categoría
	
	private float[] futureCoords;	//almacena las coordenadas futuras de los mensajes.
	private boolean calculateFutureCoords = true;
	
	private GLModel lineModel;	//el modelo de las lineas para cada categoría
	private float[] lineCoords;	//coordenadas de las lineas
	private float[] lineColors;	//colores de las lineas
	
	private float[] futureLineCoords;	//almacena las coordenadas futuras
	
	public ForumCategory(){
		name = "";
		subCategories= new ArrayList<ForumCategory>();
		threads = new TreeMap<String,ForumThread>();
		threadsByDate = new TreeMap<Date, ForumThread>();
	}
	
	public ForumCategory(String name){
		this.name = name;
		subCategories= new ArrayList<ForumCategory>();
		threads = new TreeMap<String, ForumThread>();
		threadsByDate = new TreeMap<Date, ForumThread>();
	}
	
	public float getHue() {
		return hue;
	}

	public void setHue(float hue) {
		this.hue = hue;
	}

	public float getSat() {
		return sat;
	}

	public void setSat(float sat) {
		this.sat = sat;
	}

	public int getMessagesNumber() {
		return messagesNumber;
	}

	public void setMessagesNumber(int messagesNumber) {
		this.messagesNumber = messagesNumber;
	}

	public String getName() {
		return name;
	}

	public void setName(String name) {
		this.name = name;
	}

	public ArrayList<ForumCategory> getSubCategories() {
		return subCategories;
	}

	public void setSubCategories(ArrayList<ForumCategory> subCategories) {
		this.subCategories = subCategories;
	}
	
	public TreeMap<String, ForumThread> getThreads() {
		return threads;
	}

	public void setThreads(TreeMap<String, ForumThread> threads) {
		this.threads = threads;
	}

	public void addSubCategory(ForumCategory subCategory){
		subCategories.add(subCategory);
	}
	
	public void addThread(ForumThread thread){
		if(thread!=null){
			threads.put(thread.getId(),thread);
			threadsByDate.put(thread.getDate(), thread);
		}
	}

	public TreeMap<Date, ForumThread> getThreadsByDate() {
		return threadsByDate;
	}

	public void setThreadsByDate(TreeMap<Date, ForumThread> threadsByDate) {
		this.threadsByDate = threadsByDate;
	}

	public ForumCategory getParentCategory() {
		return parentCategory;
	}

	public void setParentCategory(ForumCategory parentCategory) {
		this.parentCategory = parentCategory;
	}

	public float[] getCoords() {
		return coords;
	}

	public void setCoords(float[] coords) {
		this.coords = coords;
	}

	public float[] getColors() {
		return colors;
	}

	public void setColors(float[] colors) {
		this.colors = colors;
	}

	public GLModel getModel() {
		return model;
	}

	public void setModel(GLModel model) {
		this.model = model;
	}
	
	public GLModel getLineModel() {
		return lineModel;
	}

	public void setLineModel(GLModel lineModel) {
		this.lineModel = lineModel;
	}

	public float[] getFutureCoords() {
		return futureCoords;
	}

	public void setFutureCoords(float[] futureCoords) {
		this.futureCoords = futureCoords;
	}
	
	public boolean isCalculateFutureCoords() {
		return calculateFutureCoords;
	}

	public void setCalculateFutureCoords(boolean calculateFutureCoords) {
		this.calculateFutureCoords = calculateFutureCoords;
	}

	public float[] getLineCoords() {
		return lineCoords;
	}

	public void setLineCoords(float[] lineCoords) {
		this.lineCoords = lineCoords;
	}

	public float[] getLineColors() {
		return lineColors;
	}

	public void setLineColors(float[] lineColors) {
		this.lineColors = lineColors;
	}

	public float[] getFutureLineCoords() {
		return futureLineCoords;
	}

	public void setFutureLineCoords(float[] futureLineCoords) {
		this.futureLineCoords = futureLineCoords;
	}

	public int getId() {
		return id;
	}

	public void setId(int id) {
		this.id = id;
	}
	
	public String toXml(){
		String result = "";
		
		StringBuffer buffer = new StringBuffer();
		
		buffer.append("\t<category>\n");
		buffer.append("\t\t<categoryName>");
		buffer.append(StringEscapeUtils.escapeXml(this.name));
		buffer.append("</categoryName>\n");
		
		buffer.append("\t\t<categoryId>");
		buffer.append(this.getId());
		buffer.append("</categoryId>\n");
		
		buffer.append("\t\t<categoryMessageNumber>");
		buffer.append(this.messagesNumber);
		buffer.append("</categoryMessageNumber>\n");
		
		if(subCategories!=null && subCategories.size()>0){
			buffer.append("\t\t<subcategories>\n");
			for(ForumCategory category : subCategories){
				buffer.append(category.toXml());
			}
			buffer.append("\t\t</subcategories>");
		}
		
		if(threads!=null && threads.size()>0){
		
			buffer.append("\t\t<threads>\n");
			Collection<ForumThread> collectionThread = threads.values();
			
			Iterator<ForumThread> iterator = collectionThread.iterator();
			
			while(iterator.hasNext()){
				ForumThread thread = iterator.next();
				
				buffer.append(thread.toXml());
			}
			
			buffer.append("\t\t</threads>\n");
		}
		
		buffer.append("\t</category>\n");
		
		result = buffer.toString();
		
		return result;
	}

	public String toString(){
		String result = "";
		
		StringBuffer buffer = new StringBuffer();
		
		buffer.append("[name:");
		buffer.append(name);
		for(ForumCategory subCategory:subCategories){
			buffer.append("\n\t category:");
			buffer.append(subCategory);
		}
		
		Collection<ForumThread> collection = threads.values();
		
		//aqui imprimo los threads de la categoría
		for(ForumThread thread : collection){
			buffer.append("\n\t thread:");
			buffer.append(thread);
		}
		
		buffer.append("]");
		
		result = buffer.toString();
		
		return result;
	}
	
}
